//
//  HistoryViewController.m
//  Ten
//
//  Created by anfuer on 2018/8/16.
//  Copyright © 2018年 xg韦. All rights reserved.
//

#import "HistoryViewController.h"
#import "HomeTbvCell.h"
#import "SPPageMenu.h"
#import "companyCell.h"
@interface HistoryViewController ()<SPPageMenuDelegate, UIScrollViewDelegate>
{
    int pag;
    NSMutableArray*mary1;
    NSMutableArray*mary2;
}
@property (weak, nonatomic) IBOutlet UIView *muneBack;
@property (weak, nonatomic) IBOutlet UIScrollView *scrview;
@property (weak, nonatomic) IBOutlet UITableView *tbv1;
@property (weak, nonatomic) IBOutlet UITableView *tbv2;
@property (weak, nonatomic) IBOutlet UIView *hidenview1;
@property (weak, nonatomic) IBOutlet UIView *hidenview2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrW;
@property (nonatomic, weak) SPPageMenu *pageMenu;

@end

@implementation HistoryViewController


- (IBAction)cleanBtna:(UIButton *)sender {
    [FuncPublic getData:self parameters:@{@"type":[NSString stringWithFormat:@"%ld",_pageMenu.selectedItemIndex+1]} RequestUrl:Xclear_browses success:^(NSDictionary *acountInfo) {
       [WSProgressHUD showImage:nil status:@"清理成功"];
          if (_pageMenu.selectedItemIndex==0) {
              [_tbv1.mj_header beginRefreshing];
          }else
          {
              [_tbv2.mj_header beginRefreshing];
          }
    }];

    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    _tbv1.separatorColor=[UIColor clearColor];
    _tbv2.separatorColor=[UIColor clearColor];
    _scrW.constant=DEVW*2;
    // trackerStyle:跟踪器的样式
    SPPageMenu *pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0, DEVW, 40) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
    // 传递数组，默认选中第2个
    [pageMenu setItems:@[@"职位",@"公司"] selectedItemIndex:0];
    pageMenu.permutationWay=SPPageMenuPermutationWayNotScrollEqualWidths;
    // 设置代理
    pageMenu.delegate = self;
    [_muneBack addSubview:pageMenu];
    _pageMenu = pageMenu;
    
    
    _tbv1.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        pag=1;
        mary1=[NSMutableArray array];
        [self loadData];
    }];
    _tbv2.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        pag=1;
        mary2=[NSMutableArray array];
        [self loadData];
    }];
  

    
    
}
-(void)loadData{
    [FuncPublic getData:self parameters:@{@"type":[NSString stringWithFormat:@"%ld",_pageMenu.selectedItemIndex+1]} RequestUrl:Xmybrowses success:^(NSDictionary *acountInfo) {
        NSLog(@"%@",acountInfo);
        [_tbv1.mj_header endRefreshing];
        [_tbv2.mj_header endRefreshing];
        if (_pageMenu.selectedItemIndex==0) {
             mary1=[NSMutableArray arrayWithArray:acountInfo[@"data"]];
            [_tbv1 reloadData];
            _hidenview1.hidden=mary1.count>0?YES:NO;
        }else
        {
            mary2=[NSMutableArray arrayWithArray:acountInfo[@"data"]];
            [_tbv2 reloadData];
            _hidenview2.hidden=mary2.count>0?YES:NO;
        }

    }];
    
}


#pragma mark - scrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView==_scrview) {
        [self.pageMenu moveTrackerFollowScrollView:scrollView];
    }
    
}

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    if (labs(toIndex - fromIndex) >= 2) {
        [_scrview setContentOffset:CGPointMake(DEVW * toIndex, 0) animated:NO];
    } else {
        [_scrview setContentOffset:CGPointMake(DEVW * toIndex, 0) animated:YES];
    }
    pag=1;
    mary1=[NSMutableArray array];
    mary2=[NSMutableArray array];
    [self loadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 返回分组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _pageMenu.selectedItemIndex==0? mary1.count:mary2.count;
}

#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if (_pageMenu.selectedItemIndex==0) {
        HomeTbvCell*cell=[HomeTbvCell CellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary*tempdic=mary1[indexPath.section];
        [cell Cellinit:tempdic[@"job_detail"]];
        return cell;
    }else
    {
        companyCell*cell=[companyCell CellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary*tempdic=mary2[indexPath.section];
        [cell Cellinit:tempdic[@"company_detail"]];
        return cell;
    }
    
    
}
#pragma mark 设置分组头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}


#pragma mark 设置尾部说明内容高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

#pragma mark 设置每行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [HomeTbvCell FSCellHeightForTableView:tableView indexPath:indexPath cacheKey:@"HomeTbvCell" cellContentViewWidth:0 bottomOffset:0];
}
#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
     if (_pageMenu.selectedItemIndex==0) {
        [MyUtils collviewSelidex:@"职位详情" toVc:self andShopInfo:mary1[indexPath.section]];
     }else
     {
        [MyUtils collviewSelidex:@"公司详情" toVc:self andShopInfo:mary2[indexPath.section][@"company_detail"]];
     }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
  if (_pageMenu.selectedItemIndex==0) {
    return [FuncPublic time:mary1[section][@"add_time"]];
  }else
  {
       return [FuncPublic time:mary2[section][@"add_time"]];
  }
}



@end
